package com.ruge.test.Velocity;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.junit.Test;

import java.io.StringWriter;

/**
 * 爱丽丝、如歌  创建于 2018/7/10 20:29
 * 说明:  TODO
 */
public class Velociry_01_HelloWorld {
    @Test
    public void test1(){

        VelocityEngine velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocityEngine.init();

        VelocityContext context = new VelocityContext();
        context.put("hello","HelloWorld");
        context.put("who","刘德华");

        String str = "velocity/Velocity_01_helloworld.vm";
        Template template = velocityEngine.getTemplate(str);

        StringWriter stringWriter = new StringWriter();
        template.merge(context,stringWriter);
        stringWriter.flush();
        System.out.println(stringWriter.toString());
    }
}
